package handlers

import (
	"context"
	"douyin_mini/cmd/api/rpc"
	"douyin_mini/kitex_gen/user"
	"douyin_mini/pkg/errno"
	"github.com/cloudwego/hertz/pkg/app"
)

// Register register user info
// 这也是一个处理http请求的函数
// 作用是注册
func Register(ctx context.Context, c *app.RequestContext) {
	username := c.Query("username")
	password := c.Query("password")

	//var registerVar UserParam
	//if err := c.Bind(&registerVar); err != nil {
	//	SendResponse(c, errno.ConvertErr(err), nil)
	//	return
	//}

	if len(username) == 0 || len(password) == 0 {
		SendResponse(c, errno.ParamErr, nil)
		return
	}

	err := rpc.CreateUser(context.Background(), &user.RegisterUserRequest{
		Username: username,
		Password: password,
	})
	if err != nil {
		SendResponse(c, errno.ConvertErr(err), nil)
		return
	}
	//newClient, err := client.NewClient()
	//if err != nil {
	//	SendResponse(c, errno.ConvertErr(err), nil)
	//	return
	//}

	//_, body, _ := newClient.Post(ctx, []byte{}, "http://120.0.0.1:8080/login?username="+username+"&password="+password, &protocol.Args{})
	//
	//var userParam UserParam
	//json.Unmarshal(body, &userParam)
	SendResponse(c, errno.Success, nil)
}
